﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Common.UI;
using System.Xml.Linq;
using Microsoft.Practices.Composite.Presentation.Commands;
using System.Threading;
using System.Windows.Threading;

namespace CBE.UI.Shell
{
    /// <summary>
    /// Interaction logic for Shell.xaml
    /// </summary>
    public partial class Shell : Window, IMainView
    {
        public Shell()
        {
            InitializeComponent();
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            MenuItem menuItem = (MenuItem)e.Source;
            if (menuItem.Tag != null)
            {
                if (menuItem.Tag.ToString() == "Salir")
                {
                    this.Close();
                }
                else
                {
                    var strMenu = menuItem.Tag.ToString();

                    controllerType = Type.GetType(strMenu);

                    Thread controllerThread = new Thread(new ThreadStart(
                        delegate
                        {
                            Dispatcher.Invoke(DispatcherPriority.Normal, new Action<object>(ControllerInvoke), null);
                        }
                    ));

                    controllerThread.Start();

                }
            }
        }

        Type controllerType;

        private void ControllerInvoke(object o)
        {
            var controller = controllerType.GetConstructor(Type.EmptyTypes).Invoke(null);

            controllerType.GetMethod("Run").Invoke(controller, null);
        }

        #region IMainView Members

        public void RegisterView(string titulo, object view)
        {
            var item = new TabItem
            {
                Content = view,
                Header = titulo,
            };
            item.HeaderTemplate = (DataTemplate)this.FindResource("ClosableTabItemTemplate");
            item.DataContext = this;
            this.mainContent.Items.Add(item);
            item.IsSelected = true;
        }

        #endregion

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var tabitem = Common.UI.Controls.UIHelper.FindVisualParent<TabItem>(sender as DependencyObject);
            this.mainContent.Items.Remove(tabitem);
        }
    }

    public class MenuItemWithSecurity : MenuItem
    {
        public MenuItemWithSecurity()
            : base()
        {
            this.Loaded += new RoutedEventHandler(MenuItemWithSecurity_Loaded);
        }

        void MenuItemWithSecurity_Loaded(object sender, RoutedEventArgs e)
        {
            if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                return;
            }
            if (this.Tag != null)
            {
                if (!SEG.Authorization.CanExecuteManager(this.Tag.ToString()))
                {
                    this.Visibility = Visibility.Collapsed;
                }
            }
        }
    }
}
